package com.example.service.impl;

import com.example.entity.ComplaintUsers;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Staffs;
import com.example.mapper.ComplaintUsersMapper;
import com.example.mapper.StaffsMapper;
import com.example.result.CommonResult;
import com.example.service.ComplaintUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author: tobed
 * @date: 2022/11/17
 * @time: 20:52
 */
@Service
public class ComplaintUsersServiceImpl implements ComplaintUsersService {

    @Autowired
    ComplaintUsersMapper mapper;

    @Autowired
    StaffsMapper staffsMapper;

    /*全查*/
    @Override
    public CommonResult list(PageRequest pageRequest, ComplaintUsers complaintUsers) {
        /*PageHelper分页*/
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(ComplaintUsers.class);
        if (!complaintUsers.getComplaintClassify().isEmpty()){
            example.createCriteria().andLike("complaintClassify",complaintUsers.getComplaintClassify());
        }
        List<ComplaintUsers> list = mapper.selectByExample(example);
        /*查询出来的list转化为流形式遍历*/
        list.stream()
                .forEach(complaintUsers1 -> {
                    /*如果添加人id不为空时去查名称*/
                    if (complaintUsers1.getComplaintUserId()!=null && complaintUsers1.getComplaintUserId()!=0){
                        Staffs staffs = staffsMapper.getPeoppeById(complaintUsers1.getComplaintUserId());
                        /*将名称添加到当前的complaintUser*/
                        complaintUsers1.setComplaintUser(staffs.getStaffName());
                    }
                });
        /*返回PageHelper分页结果*/
        return CommonResult.ok(MyPageHelper.getPageResult(list));
    }

    /*删除*/
    @Override
    public CommonResult remove(Integer id) {
        int i = mapper.deleteByPrimaryKey(id);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*修改*/
    @Override
    public CommonResult update(ComplaintUsers complaintUsers) {
        complaintUsers.setUpdateTime(new Date());
        int i = mapper.updateByPrimaryKeySelective(complaintUsers);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*新增*/
    @Override
    public CommonResult create(ComplaintUsers complaintUsers) {
        complaintUsers.setCreateTime(new Date());
        complaintUsers.setStatus(0);
        int i = mapper.insertSelective(complaintUsers);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }
}
